import { createRouter, createWebHashHistory } from 'vue-router'
import {getToken} from '@/utils/token'
// import useUserInfo from '@/stores/useUserInfo'

const asyncRouter=[
  {
  path: '/',
  redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/Home/Home.vue'),
    meta: {
      isShowNav: true,
    },
  },
  {
    path: '/recommend',
    name: 'recommend',
    component: () => import('@/views/Recommend/Recommend.vue'),
    meta: {
      isShowNav: true,//底部tabbar组件的显示
      // title:'种草文章',//头部vant组件的title
      needLogin:true//登录才有权限访问的路由
    },
  },
  {
    path: '/add-article',
    name: 'addArticle',
    component: () => import('@/views/Recommend/Add.vue'),
    meta: {
      isShowNav: true,
      title:'新增文章',
      needLogin:true
    },
  },
  {
    path: '/article-detail',
    name: 'articleDetail',
    component: () => import('@/views/Recommend/Detail.vue'),
    meta: {
      title: '种草文章详情',
      needLogin: true,
    },
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/Cart/Cart.vue'),
    meta: {
      isShowNav: true,
      title:'购物车'
    },
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('@/views/User/User.vue'),
    meta: {
      isShowNav: true,
      title:'我的'
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login.vue'),
    meta: {
      isShowNav: false,
    },
  },
  {
    path: '/goods/:id',
    name: 'goods',
    component: () => import('@/views/Home/GoodDetail.vue'),
    meta: {
      title:'商品详情',
      needLogin:true//登录才有权限访问的路由
    },
  },
  {
    path: '/address-list',
    name: 'addressList',
    component: () => import('@/views/Address/Address.vue'),
    meta: {
      title:'收货地址', //顶部标题
      needLogin:true//登录才有权限访问的路由
    },
  },
  {
    path: '/edit-address',
    name: 'editressList',
    component: () => import('@/views/Address/EditAddress.vue'),
    meta: {
      needLogin:true
    },
  },
  {
    path:'/create-order',
    name: 'createOrder',
    component: () => import('@/views/Order/Created.vue'),
    meta: {
      needLogin: true,
      title:'生成订单'
    },
  },
  {
    path:'/order',
    name: 'order',
    component: () => import('@/views/Order/List.vue'),
    meta: {
      needLogin: true,
      title:'我的订单'
    },
  },
  {
    path:'/thumbs-up',
    name: 'thumbsUp',
    component: () => import('@/views/User/ThumbsUp.vue'),
    meta: {
      needLogin: true,
      title:'我的点赞'
    },
  },
  {
    path:'/my-collection',
    name: 'myCollection',
    component: () => import('@/views/User/Collection.vue'),
    meta: {
      needLogin: true,
      title:'我的收藏'
    },
  },
  {
    path:'/my-message',
    name: 'myMessage',
    component: () => import('@/views/User/MyMessage.vue'),
    meta: {
      needLogin: true,
      title:'我的信息'
    },
  },
  {
    path:'/my-password',
    name: 'myPassword',
    component: () => import('@/views/User/MyPassword.vue'),
    meta: {
      needLogin: true,
      title:'修改密码'
    },
  },
  // 访问没有的路由直接跳往首页
  {
    path: '/:toHome*',
    name: 'toHome',
    redirect: '/home',
  },
]

// let store=useUserInfo();

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
   ...asyncRouter
  ]
})

// 路由前置守卫
router.beforeEach((to,from,next)=>{
  // cookie里有token
  if(getToken()){
    // 登录了 还要去login 页面跳回去
    if(to.path==='/login'){
      next(from.path)
    }else{
      // 去其他地方随便去
      next()
    }
  }else{
    // 没登陆 有的页面没登录也有权限访问 需要判断
    if(to.meta.needLogin){
      next('/login');
    }else{
      next();
    } 
  }
})

export default router